<?php
/**
 * this is cloud search http request class test file
 *
 *
 */
require_once 'index.php';
require_once 'Framework/TestCase.php';
require_once(DIR_LIBRARY."orm".DS."acl".DS."permission.php");
class AccessTest extends PHPUnit_Framework_TestCase 
{
    private $className = "AclAccess";
    private static $config = array(
        "user_id"   => 1,
        "access_id" => 1,    
    );

    public function testPermission()
    {
        $user_id   = self::$config["user_id"];
        $access_id = self::$config["access_id"]; 

        $bool = AclPermission::isAllow($user_id, $access_id);
        $this->assertTrue($bool);

        $bool = AclPermission::isAllow($user_id, 10000);
        $this->assertFalse($bool);
    }

    /**
     * test case, function name begin with  test, e.g.testSum
     * or use @test for not begin with test.
     *
     */
    public function testCreateAccess()
    {
        $name    = "acl_test";
        $type_id = AclAccess::TYPE_MENU; 
        $parent_id = 0;
        $link = "/admin/acl/acl_test";

        $access = $this->createAccess($name, $type_id, $parent_id, $link);
        $this->assertTrue((bool)$access->getId());

        $access = new AclAccess($access->getId());
        $this->assertTrue((bool)$access->getId());
        $this->assertTrue($access->getField("parent_id") == $parent_id);
        $this->assertTrue($access->getField("type_id") == $type_id);
        $this->assertTrue($access->getName() == $name);
        $this->assertTrue($access->getLink() == $link);
        $this->assertTrue(1 == $access->getLevel());

        $parent_id = $access->getId();
        $access = $this->createAccess($name, $type_id, $parent_id, $link);
        $this->assertTrue((bool)$access->getId());

        $access = new AclAccess($access->getId());
        $this->assertTrue((bool)$access->getId());
        $this->assertTrue($access->getField("parent_id") == $parent_id);
        $this->assertTrue($access->getField("type_id") == $type_id);
        $this->assertTrue($access->getName() == $name);
        $this->assertTrue(2 == $access->getLevel());
    }

    public function createAccess($name, $type_id, $parent_id, $link)
    {
        //ORMFactory::get("acl.acl_access");
        $orm = new AclAccess();
        $orm->setName($name);
        $orm->setField("type_id", $type_id);
        $orm->setField("parent_id", $parent_id);
        $orm->setLink($link);
        $orm->create();

        return $orm;
    }

    public function testUpdateAccess()
    {
        $access = $this->createAccess();
        $id = $access->getId();

        $access = new AclAccess($access->getId());
        $name = "abc";
        $orm->setName($name);
        $orm->appendWhere("id = ".$id);
        $orm->update();

        $access = new AclAccess($access->getId());
        $this->assertEquals($access_id->getName(), $name);
    }


    public function testDelete()
    {
        $access = $this->createAccess();
        $id = $access->getId();

        $access = new AclAccess($access->getId());
        $orm->appendWhere("id = ".$id);
        $orm->delete();

        $access = new AclAccess($access->getId());
        $this->assertFalse($access_id->getId());
    }

    public function __testGetMenu()
    {
        $orm = new $this->className();
        $sql = 'INSERT INTO '.$orm->getTableName().' VALUES (1, "read", 0, 1, 1, 0);';
        $orm->query($sql);
        $sql = 'INSERT INTO '.$orm->getTableName().' VALUES (2, "update", 0, 1, 1, 0);';
        $orm->query($sql);
        $sql = 'INSERT INTO '.$orm->getTableName().' VALUES (4, "delete", 1, 1, 2, 1);';
        $orm->query($sql);
        $sql = 'INSERT INTO '.$orm->getTableName().' VALUES (8, "create", 2, 1, 2, 2);';
        $orm->query($sql);
        $sql = 'INSERT INTO '.$orm->getTableName().' VALUES (16, "list", 0, 2, 1, 0);';
        $orm->query($sql);

        $expect = array(
            1 => array(
                "id" => 1,
                "name" => "read",
                "parent_id" => 0,
                "type" => 1,
                "level" => 1,
                "order_list" => 0    
            ),
            2 => array(
                
            ),    
        );

        $menu = $orm->getMenu();
    }

    public function truncateTable()
    {
        $orm = new $this->className();
        $sql = "TRUNCATE TABLE ".$orm->getTableName();

        $rs = $orm->query($sql);
        if (!$rs->isSuccess())
        {
            throw new Exception("query fail");
        }
        echo "truncate table ".$table_name."\r\n";
    }
    /**
     * execute before class
     *
     */
    public static function setUpBeforeClass()
    {

    }

    /**
     * execute before case
     *
     */
    protected function setUp()
    {

    }

    /**
     *
     * execute before assert
     */
    protected function assertPreConditions()
    {

    }

    /**
     *
     * execute after assert
     */
    protected function assertPostConditions()
    {

    }

    /**
     * execute after case
     */
    protected function tearDown()
    {

    }

    /**
     * execute after class
     *
     */
    public static function tearDownAfterClass()
    {

    }


}//end class
